package com.example.leo.mycharmingstudent.project.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.project.entity.KnowledgeTreeEntity;

import java.util.List;

/**
 * Created by Leo on 2020/9/29.
 */

public class KnowledgeAdapter extends RecyclerView.Adapter<KnowledgeAdapter.KnowledgeHolder> {

    private Context mContext;
    private List<KnowledgeTreeEntity.DataBean> mList;

    public KnowledgeAdapter(Context context, List<KnowledgeTreeEntity.DataBean> list) {
        this.mContext = context;
        this.mList = list;
    }

    /**
     * 创建视图  既然是创建视图，就需要Context
     * 返回ViewHolder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public KnowledgeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_knowledge_adapter, parent, false);
        return new KnowledgeHolder(view);
    }

    /**
     * 设置数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final KnowledgeHolder holder, int position) {
        final KnowledgeTreeEntity.DataBean dataBean = mList.get(position);
        String name = dataBean.getName();
        /*
        holder可以看成对象，对象访问自己的变量，没毛病
         */
        holder.tvTitle.setText(name);
        List<KnowledgeTreeEntity.DataBean.ChildrenBean> children = dataBean.getChildren();
        int size = children.size();
        /*
        StringBuilder和StringBuffer的区别
        1.StringBuilder效率高
        2.StringBuffer是线程安全的
         */
        StringBuilder contentBuilder = new StringBuilder();
        for (int i = 0; i < size; i++) {
            contentBuilder.append(children.get(i).getName()).append("   ");
        }
        holder.tvContent.setText(contentBuilder.toString());

        if (mListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //要干什么？
                    //执行OnKnowledgeItemClickListener对象的onItemClick方法

                    /*
                    mListener是OnKnowledgeItemClickListener的对象
                    onItemClick属于OnKnowledgeItemClickListener对象的方法
                    所以，mListener调用onItemClick方法没毛病
                     */
                    mListener.onItemClick(holder.getAdapterPosition(), dataBean);
                }
            });
        }
    }

    /**
     * 返回需要加载的数据条目数量
     * 既然想到数据条目数量，就要考虑传进来数据的List列表
     * 既然想到数据的List列表，就要考虑到json实体类
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class KnowledgeHolder extends RecyclerView.ViewHolder {

        /**
         * tvTitle属于对象的
         * 只有添加了static关键字的变量或方法，才属于类，而且，内存中只存在一个
         */
        //标题
        TextView tvTitle;
        //内容
        TextView tvContent;

        public KnowledgeHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title_item_knowledge_adapter);
            tvContent = itemView.findViewById(R.id.tv_content_item_knowledge_adapter);
        }
    }

    private OnKnowledgeItemClickListener mListener;

    public void setOnKnowledgeItemClickListener(OnKnowledgeItemClickListener listener){
        this.mListener = listener;
    }

    /**
     * 知识体系条目点击事件
     */
    public interface OnKnowledgeItemClickListener {
        /**
         * 知识体系条目点击以后，执行的方法
         * @param position 点击条目的位置
         * @param dataBean 条目的数据
         */
        void onItemClick(int position, KnowledgeTreeEntity.DataBean dataBean);
    }
}
